<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2020/4/27
 * Time: 15:56
 */

namespace App\Services;

use App\Models\PatentEvaluate;
use Illuminate\Mail\Mailer;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;

class MailService
{
    //评估发送邮件
    public function evaluateSend(PatentEvaluate $evaluate)
    {
        if(!$evaluate->file){
            return false;
        }
        try{
            Mail::send('emails.evaluate-email',['evaluate'=>$evaluate],function(Message $message) use ($evaluate){
                $to = config('app.to_mail');
                $to = '540124900@qq.com';
                $message ->to($to)->subject('专利评估-'.$evaluate->patent_name);
//                if($evaluate->file && Storage::disk('admin')->exists($evaluate->file)){
//                    $extension = last(explode('.',$evaluate->file));
//                    $message->attach(public_path('uploads/').$evaluate->file,['as'=>'专利技术交底书.'.$extension]);
//                }
            });
            // 返回的一个错误数组，利用此可以判断是否发送成功
            $res = Mail::failures();
            if($res){
                Log::info('【邮件-发送失败】',$res);
            }
        }catch (\Exception $exception){
            Log::info('【邮件配置-异常】',[$exception->getMessage()]);
        }

    }
    public function send()
    {
        $name = '我发的第一份邮件';
        // Mail::send()的返回值为空，所以可以其他方法进行判断
        Mail::send('emails.evaluate-email',['name'=>$name],function(Message $message){
            $to = 'jwb18037773471@126.com';
            $message ->to($to)->attach(storage_path('app/files/2/专利技术交底书_1.docx'),['as'=>'测试附件.docx'])->subject('邮件测试');
        });
        // 返回的一个错误数组，利用此可以判断是否发送成功
        dump(Mail::failures());
    }
}